package dao.Impl;

import dao.NavDao;
import dto.NavModelDto;
import model.NavModel;
import util.JDBC;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class NavImpl implements NavDao {
//    @Override
//    public NavModelDto getNavModelDto() {
//        String sql="";
//        ResultSet resultSet=JDBC.getResultSet(sql);
//        NavModelDto navModelDto= new NavModelDto();
//        try {
//            while (resultSet.next()){
//
//            }
//        }catch (Exception e){
//
//        }
//        return null;
//    }

    /**
     * 封装sql语句,
     * 执行JDBC代码
     *
     * @param navModelDto
     * @return
     */
    @Override
    public int add(NavModelDto navModelDto) {
        String sql = "INSERT INTO `hs_nav`" +
                " ( `href`, `title`,`status`) " +
                "VALUES ( '" + navModelDto.getHref() + "', " +
                "'" + navModelDto.getTitle() + "'," +
                " '"+ '1'+ "',);";

        return JDBC.excuteUpdate(sql);
    }

    @Override
    public List<NavModel> getNavModelList() {

        String sql = "select * from `hs_nav` where status=1";//封装sql语句
        ResultSet resultSet = JDBC.getResultSet(sql);
        List<NavModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                NavModel model = new NavModel();
                model.setId(resultSet.getInt("id"));
                model.setHref(resultSet.getString("href"));
                model.setTitle(resultSet.getString("title"));
                model.setStatus(resultSet.getInt("status"));
                list.add(model);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBC.close();
        }
        return list;
    }

    @Override
    public int del(NavModel navModel) {
        String sql = "delete from `hs_nav`  where id=" + navModel.getId();
        return JDBC.excuteUpdate(sql);
    }

    @Override
    public NavModel findById(int id) {

        String sql = "select * from `hs_nav`  where id=" + id;//封装sql语句
        ResultSet resultSet = JDBC.getResultSet(sql);
        NavModel model = new NavModel();
        try {
            while (resultSet.next()) {

                model.setId(resultSet.getInt("id"));
                model.setHref(resultSet.getString("href"));
                model.setTitle(resultSet.getString("title"));
                model.setStatus(resultSet.getInt("status"));

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int update(NavModel navModel) {
        String sql="update from * `hs_nav`  set " +
                "href='" +navModel.getHref()+"'," +
                "title='"+navModel.getTitle()+"',"+
                "status="+navModel.getStatus()+"where id="+navModel.getId();
        return JDBC.excuteUpdate(sql);
    }

}
